<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row class="search-input">
          <el-form-item label="创建时间始">
            <el-date-picker
              v-model="filters.startTime"
              type="date"
              placeholder="创建时间始"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 200px;"
              v-on:input="checkStartTime"
            ></el-date-picker>
          </el-form-item>

          <el-form-item label="创建时间末">
            <el-date-picker
              v-model="filters.endTime"
              type="date"
              placeholder="创建时间末"
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              style="width: 200px;"
              v-on:input="checkEndTime"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="等级名称">
            <el-input v-model.trim="filters.levelName" style="width: 200px;" placeholder="等级名称"></el-input>
          </el-form-item>
          <el-row class="search-button">
            <el-form-item>
              <kt-button
                icon="fa fa-search"
                :label="$t('action.search')"
                type="primary"
                @click="findPage(null)"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                icon="fa fa-plus"
                :label="$t('action.add')"
                type="primary"
                @click="handleAdd"
              />
            </el-form-item>
            <el-form-item>
              <kt-button
                icon="fa fa-hand-pointer-o"
                :label="$t('action.reset')"
                type="primary"
                @click="resetForm('filters')"
              />
            </el-form-item>
          </el-row>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel" id="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <Jic-table
      :height="350"
      :tableList="pageResult"
      :tableHeaderList="filterColumns"
      @findPage="findPage"
      :showBatchDelete="false"
      :showEditOperation="true"
      :showDeleteOperation="false"
      @handleDelete="handleDelete"
      :totalSize="totalSize"
      @handleEdit="handleEdit"
      @handlePageChange="handlePageChange"
      @handleSizeChange="handleSizeChange"
      :showSelection="false"
      :sortable="false"
      :showIndex="false"
      ref="JicTable"
    ></Jic-table>
    <!--新增编辑界面-->
    <el-dialog
      :title="operation?'新增':'编辑'"
      width="40%"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      >
      <el-form
        :model="dataForm"
        label-width="85px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        label-position="right"
      >
        <el-form-item label="数值" prop="growthRuleVal">
          <el-input v-model.trim="dataForm.growthRuleVal" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员id" prop="membeId">
          <el-input
            v-model.trim="dataForm.membeId"
            style="width:230px"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxlength="5"
            auto-complete="off"
            :disabled="rangeDis"
          ></el-input>-
          <el-input
            v-model.trim="dataForm.growRangeEnd"
            auto-complete="off"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxlength="5"
            min="0"
            style="width:230px"
            :disabled="rangeDis"
          ></el-input>
        </el-form-item>
        <el-form-item label="每年扣除" prop="yearDeduct">
          <el-input
            v-model.trim="dataForm.yearDeduct"
            auto-complete="off"
            oninput="value=value.replace(/[^\d]/g,'')"
            maxlength="5"
            min="0"
            style="width:300px;"
          ></el-input>成长值
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :size="size" @click.native="dialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import PopupTreeInput from "@/components/PopupTreeInput";
import JicTable from "@/components/jicComponentGallery/jicTable";
import KtButton from "@/views/Core/KtButton";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
import { format } from "@/utils/datetime";
import { exportExcel } from "@/utils/excel";
export default {
  components: {
    PopupTreeInput,
    JicTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      //成长值范围 操作控件
      rangeDis:false,
      totalSize: 0,
      size: "small",
      filters: {
        levelName: null,
        endTime: null,
        startTime: null
      },
      columns: [],
      filterColumns: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: [],
      options: {},
      operation: false, // true:新增, false:编辑
      dialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        levelName: [
          { required: true, message: "请输入等级名称", trigger: "blur" }
        ]
        // growRange: [
        //   { required: true, message: "请输入成长值范围", trigger: "blur" }
        // ],
      },

      // 新增编辑界面数据
      dataForm: {}
    };
  },
  methods: {
    //时间判断
    checkStartTime: function() {
      let this_ = this;
      let minTime = new Date(this_.filters.startTime).getTime();
      let maxTime = new Date(this_.filters.endTime).getTime();

      if (minTime > maxTime && maxTime != 0) {
        this_.$message({ message: "起始时间不能大于结束时间 ", type: "error" });
        this_.filters.startTime = "";
      }
    },

    checkEndTime: function() {
      let this_ = this;
      let minTime = new Date(this_.filters.startTime).getTime();
      let maxTime = new Date(this_.filters.endTime).getTime();
      if (maxTime < minTime) {
        this_.$message({ message: "结束时间不能小于起始时间 ", type: "error" });
        this_.filters.endTime = "";
      }
    },
    // 获取分页数据
    findPage: function(data) {
      let this_ = this;
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = this.pageRequest.pageNum;
      this_.filters.limit = this.pageRequest.pageSize;
      this_.utils.request.queryMemberGrowthLogList(this_.filters, function(res) {
        this_.pageResult = res.data.rows;
        this_.totalSize = Number(res.data.total);
      });
    },

    //左右分页点击
    handlePageChange(params) {
      if (params != null && params.number != null) {
        if (params.number != null) {
          this.pageRequest.pageNum = params.number;
        }

        this.findPage();
      }
    },
    // 换页刷新
    handleSizeChange: function(pageSize) {
      if (pageSize != null) {
        this.pageRequest.pageSize = pageSize.size;
        this.findPage();
      }
    },

    // 删除
    handleDelete: function(data) {
      this.$confirm("确认删除吗？", "提示", {}).then(() => {
        var params = {};
        params.id = data.row.id;

        var this_ = this;
        this.utils.request.delMemberGrade(params, function(res) {
          if (res.data == "1") {
            this_.$message({ message: "操作成功", type: "success" });
            this_.findPage(null);
          } else {
            this_.$message({
              message: "操作失败, " + res.msg,
              type: "error"
            });
          }
        });
      });
    },
    // 显示新增界面
    handleAdd: function() {
      this.dialogVisible = true;
      this.operation = true;
      this.dataForm = {};
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "客服管理";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.dialogVisible = true;
      this.operation = false;
      this.dataForm = Object.assign({}, params.row);
      this.rangeDis = true
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          let this_ = this;
          let params = Object.assign({}, this_.dataForm);
          params.token = localStorage.getItem("token");
          if (this_.dataForm.id != null) {
            this.utils.request.editMemberGrade(params, function(res) {
              if (res.data == "1") {
                this_.$message({ message: "操作成功", type: "success" });

                this_.$refs["dataForm"].resetFields();

                this_.dialogVisible = false;

                this_.findPage(null);
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          } else {
            this.utils.request.addMemberGradeInfo(params, function(res) {
              if (res.data == "1") {
                this_.$message({ message: "操作成功", type: "success" });

                this_.$refs["dataForm"].resetFields();

                this_.dialogVisible = false;

                this_.findPage(null);
              } else {
                if (res.code == "7777") {
                  this_.$message({
                    message: "操作失败, " + res.msg,
                    type: "error"
                  });
                } else {
                  this_.$message({
                    message: "操作失败, " + res.msg,
                    type: "error"
                  });
                }
              }
            });
          }
        }
      });
    },

    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },
    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      this.columns = [
        { prop: "levelName", label: "等级名称", minWidth: 50 },
        {
          prop: "account",
          label: "成长值范围",
          minWidth: 120,
          formatter: function(row, column, cellValue, index) {
            return row.growRangeStart + "-" + row.growRangeEnd;
          }
        },
        { prop: "yearDeduct", label: "每年扣除", minWidth: 120 },

        // { prop: "deptName", label: "所属组织" },
        { prop: "createTime", label: "创建时间", minWidth: 120 }
        // {prop:"lastUpdateTime", label:"更新时间", minWidth:120, formatter:this.dateFormat}
      ];
      var temp = [];
      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    resetForm(formName) {
      this.filters = {
        levelName: null,
        endTime: null,
        startTime: null
      };
      this.findPage();
    }
  },

  mounted() {
    this.initColumns();
    this.findPage();
  }
};
</script>

<style scoped lang='scss'>
</style>